package com.miao.service.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;


@Service
public class RedisService {

    @Autowired
    private ShardedJedisPool shardedJedisPool;

    //方法一：
    private <T> T execute(Function<T,ShardedJedis> fun){
        ShardedJedis shardedJedis=null;
        try{
            //从连接池中获取jedis分片对象
            shardedJedis=shardedJedisPool.getResource();
            return fun.callbace(shardedJedis);
        }finally {
            if(null!=shardedJedis){
                //关闭，检测连接是否有效，有效则放回到连接池中，无效则重置状态
                shardedJedis.close();
            }
        }
    }

    /**
     * set
     * @param key
     * @param value
     * @return
     */
    public String set(final String key,final  String value){
        return this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callbace(ShardedJedis shardedJedis) {
                return shardedJedis.set(key,value);
            }
        });
    }

    /**
     * get
     * @param key
     * @return
     */
    public String get(final String key){
        return this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callbace(ShardedJedis shardedJedis) {
                return shardedJedis.get(key);
            }
        });
    }

    /**
     * 执行删除操作
     * @param key
     * @return
     */
    public Long del(final String key){
        return this.execute(new Function<Long, ShardedJedis>() {
            @Override
            public Long callbace(ShardedJedis shardedJedis) {
                return shardedJedis.del(key);
            }
        });
    }

    /**
     * 设置生存时间
     * @param key
     * @param seconds
     * @return
     */
    public Long expire(final String key,final Integer seconds){
        return this.execute(new Function<Long, ShardedJedis>() {
            @Override
            public Long callbace(ShardedJedis shardedJedis) {
                return shardedJedis.expire(key,seconds);
            }
        });
    }

    /**
     * 在执行set操作的同时设置生存时间，单位为：秒
     * @param key
     * @param value
     * @param seconds
     * @return
     */
    public String set(final String key,final  String value,final Integer seconds){
        return this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callbace(ShardedJedis shardedJedis) {
                String str=shardedJedis.set(key,value);
                shardedJedis.expire(key,seconds);
                return str;
            }
        });
    }




    //方法二：
    /**
     * 执行set操作
     * @param key
     * @param value
     * @return
     */
//    public String set(String key,String value){
//        ShardedJedis shardedJedis=null;
//        try{
//            //从连接池中获取jedis分片对象
//            shardedJedis=shardedJedisPool.getResource();
//            return shardedJedis.set(key,value);
//        }finally {
//            if(null!=shardedJedis){
//                //关闭，检测连接是否有效，有效则放回到连接池中，无效则重置状态
//                shardedJedis.close();
//            }
//        }
//    }
    /**
     * 执行get操作
     * @param value
     * @return
     */
//    public String get(String key){
//        ShardedJedis shardedJedis=null;
//        try{
//            //从连接池中获取jedis分片对象
//            shardedJedis=shardedJedisPool.getResource();
//            return shardedJedis.get(key);
//        }finally {
//            if(null!=shardedJedis){
//                //关闭，检测连接是否有效，有效则放回到连接池中，无效则重置状态
//                shardedJedis.close();
//            }
//        }
//    }
}
